package com.fx.controller;

import com.alibaba.fastjson.JSONObject;
import com.fx.entity.WxConfig;
import com.fx.service.WxConfigService;
import com.fx.util.Constant;
import com.fx.util.PageInfo;
import com.fx.util.Query;
import com.fx.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 
 * 
 * @author yfl
 * @email 617823298@qq.com
 * @date 2018-06-19 09:22:36
 */
 
@Controller
@RequestMapping("/wxConfig")
public class WxConfigController extends BaseController {
    @Autowired
	private WxConfigService wxConfigService;
    private static String pageTitle = "微信配置管理";
    private static String pageUrl = "/wxConfig";
	
	@GetMapping()
	String WxConfig(Model model){
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl,pageTitle, Constant.LIST_TITLE));
	    return "wxConfig/wxConfig";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public JSONObject list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<WxConfig> wxConfigList = wxConfigService.getList(query);
		int total = wxConfigService.getCount(query);
		return renderPage(total,wxConfigList);
	}
	
	@GetMapping("/add")
	String add(Model model){
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl,pageTitle,Constant.ADD_TITLE));
	    return "wxConfig/add";
	}

	@GetMapping("/edit/{flId}")
	String edit(@PathVariable("flId") String flId,Model model){
		WxConfig wxConfig = wxConfigService.getById(flId);
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl,pageTitle,Constant.EDIT_TITLE));
		model.addAttribute("wxConfig", wxConfig);
	    return "wxConfig/edit";
	}

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public R saveOrUpdate(WxConfig wxConfig) {
        String id = wxConfig.getFlId();
        int result = 0;
        if (id == null || "".equals(id)) {
            result = wxConfigService.save(wxConfig);
        } else {
            result = wxConfigService.updateById(wxConfig);
        }
        if (result != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }
	/**
	 * 批量删除
	 */
	@PostMapping( "/batchDel")
	@ResponseBody
	public R remove(@RequestParam("ids") String[] ids){
        if (wxConfigService.batchRemove(ids) != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
	}
	
}
